package com.demiroot.server;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ErrorReportResponse implements HTTPResponse {

	Throwable t;
	Config c;
	
	public ErrorReportResponse(Throwable t, Config c) {
		this.t = t;
		this.c = c;
	}

	@Override
	public byte[] getMessageAsByteArray() {
		return ServerUtils.stringToByteArray((String)getMessage());
	}

	@Override
	public int getCode() {
		return 500;
	}

	@Override
	public HeaderData getHeader() {
		return null;
	}

	@Override
	public Object getMessage() {
		Map<String, String> swap = new HashMap<String, String>();
		String st = "";
		for (StackTraceElement ste : t.getStackTrace()) {
			st +=ste + "<br>\n";
		}
		swap.put("$ERROR_REPORT", st);
		swap.put("$ERROR_NAME", t.getMessage());
		return ServerUtils.generateFileFromTemplate("Error", swap, c);
	}

	@Override
	public ResponseType getResponseType() {
		return ResponseType.STRING;
	}

	@Override
	public List<Cookie> getCookies() {
		return null;
	}

	@Override
	public Calendar cookieExpire() {
		return null;
	}

}
